import React, {useEffect} from 'react';
import {styled} from '@mui/material/styles';
import {Box, Container, Paper,} from "@mui/material";
import PubSub from 'pubsub-js'
import './SearchPage.css'
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import SearchTable from './SearchTable'
import SearchTopBar from './SearchTopBar'
import {useLocation} from "react-router-dom";
import SingersTable from "./SingersTable";
import PlayListTable from "./PlayListTable";
import AlbumTable from "./AlbumTable";

function a11yProps(index) {
    return {
        id: `simple-tab-${index}`,
        'aria-controls': `simple-tabpanel-${index}`,
    };
}
const StyledTabs = styled((props) => (
    <Tabs
        {...props}
        TabIndicatorProps={{ children: <span className="MuiTabs-indicatorSpan" /> }}

    />
))({
    '& .MuiTabs-indicator': {
        display: 'flex',
        justifyContent: 'center',
        backgroundColor: 'transparent',
    },
    '& .MuiTabs-indicatorSpan': {
        maxWidth: 40,
        width: '100%',
        backgroundColor: 'rgba(0,0,0,0.52)',
    },
});

const StyledTab = styled((props) => <Tab disableRipple {...props} sx={{width : 15, height : 20}} />)(
    ({ theme }) => ({
        textTransform: 'none',
        fontWeight: theme.typography.fontWeightBold,
        fontSize: theme.typography.pxToRem(12),
        marginRight: theme.spacing(-4),
        color: 'rgba(0,0,0,0.52)',
        '&.Mui-selected': {
            color: 'rgba(0,0,0,0.52)',
        },
        '&.Mui-focusVisible': {
            backgroundColor: 'rgba(100, 95, 228, 0.32)',
        },
    }),
);
function TabPanel(props) {
    const { children, value, index, ...other } = props;

    return (
        <div
            role="tabpanel"
            hidden={value !== index}
            id={`simple-tabpanel-${index}`}
            aria-labelledby={`simple-tab-${index}`}
            {...other}
        >
            {value === index && (
                <Box sx={{ p: 3 }}>
                    {children}
                </Box>
            )}
        </div>
    );
}
function RouterTable(props){
    let {main} = props;

    //console.log("routerT" + JSON.stringify(props))
    if(main === 'songs') {
        return <SearchTable {...props}/>
    }else if(main === 'singers'){
        return <SingersTable {...props}/>
    }else if(main === 'playlists'){
       return <PlayListTable {...props}/>
    }else if(main === 'album'){
        return <AlbumTable {...props}/>
    }



}
export default function SearchPage(props){
    let {state} = useLocation();

    let {keywords} = state || {keywords:""};
    const [keyword, setKeyword] = React.useState(keywords);
    const [value, setValue] = React.useState(0);

    const {main} = props;
    const handleChange = (event, newValue) => {
        setValue(newValue);
    };
    function changeKeyWorld(msg,data){
        setKeyword(data);
    }
    useEffect((r) => {
        PubSub.subscribe('changSearchKeyWorld',changeKeyWorld)
    },[])
    //console.log(keyword)
    return (
        <Container maxWidth="xl" sx={{marginTop:1}}>
            <Paper className='page' elevation={3}>
                <SearchTopBar />
                <Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
                    <StyledTabs value={value} onChange={handleChange} >
                        <StyledTab label="网易" {...a11yProps(0)} />
                        <StyledTab label="QQ" {...a11yProps(1)} />
                        <StyledTab label="咪咕" {...a11yProps(2)} />
                        <StyledTab label="酷我" {...a11yProps(3)} />
                        <StyledTab label="酷狗" {...a11yProps(4)} />
                    </StyledTabs>
                </Box>
                <TabPanel value={value} index={0}>
                    <RouterTable keywords={keyword} main={main} from='163'/>
                    
                </TabPanel>
                <TabPanel value={value} index={1}>
                    <RouterTable keywords={keyword}  main={main} from='qq'/>
                </TabPanel>
                <TabPanel value={value} index={2}>
                    <RouterTable keywords={keyword} main={main} from='migu'/>
                </TabPanel>
                <TabPanel value={value} index={3}>
                    <RouterTable keywords={keyword} main={main} from='kuwo'/>
                </TabPanel>
                <TabPanel value={value} index={4}>
                    <RouterTable keywords={keyword} main={main} from='kugou'/>
                </TabPanel>
                <Box sx={{ p: 3 }} />
            </Paper>

        </Container>
    );


}